package com.wzh.mysending.adapter;

import android.text.TextUtils;
import android.util.Log;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wzh.mysending.R;
import com.wzh.mysending.model.LocationEntity;
import com.wzutils.BigDisMailUtils;
import com.wzutils.TimeUtils;

import java.util.List;

/**
 * Created by Hu on 2017/3/17.
 */

public class LocListAdapter extends BaseQuickAdapter<LocationEntity, BaseViewHolder> {
    double lat = 0;
    double lot = 0;

    public LocListAdapter(int layoutResId, List<LocationEntity> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LocationEntity item) {
        double mi = Double.parseDouble(String.valueOf(AMapUtils.calculateLineDistance(new LatLng(lat, lot),
                new LatLng(item.getLat(), item.getLot()))));

        long itemT = TimeUtils.stringTolong(item.getCreatedAt(), TimeUtils.YEAR_MONTH_DAY_HOUR_MIN_SS);
        long timeN = TimeUtils.systemTimeLong();
        long diff = timeN - itemT;//这样得到的差值是微秒级别

        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

        helper
                .setVisible(R.id.sudu, item.getSpeed() != 0 ? true : false)
                .setText(R.id.sudu, BigDisMailUtils.turnTrim((item.getSpeed() * 3.6),1) + "公里/小时")
                .setText(R.id.time, item.getCreatedAt())
                .setText(R.id.ago, days != 0 ? (days + "天" + hours + "小时" + minutes + "分钟" + "ago") : hours != 0 ? (hours + "小时" + minutes + "分钟" + "ago") : minutes != 0 ? (minutes + "分钟" + "ago") : "刚刚")
                .setText(R.id.address, TextUtils.isEmpty(item.getAddRess()) ?
                        "点击这一栏可以查询地址信息。" : item.getAddRess())
                .setVisible(R.id.juli,lot==0&&lat==0?false:true)
                .setText(R.id.juli, mi > 500 ? BigDisMailUtils.turn(mi / 1000, 2) + "千米" : BigDisMailUtils.turn(mi, 2) + "米");


    }

    public void setLatAndLot(double lat, double lot) {
        this.lat = lat;
        this.lot = lot;
    }
}
